import { Component, OnInit, Injector, ViewChild, Input, ViewEncapsulation, Output, EventEmitter } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';
import {
    ListDtoExtensionsItem,
    StockType,
    StockZT_SupplierServiceProxy,
    Stock_SupplierEditDto,
    Stock_SupplierListDto
} from '@shared/service-proxies/stock-service-proxies';
import { CreateOrEditSupplierModalComponent } from './create-or-edit-supplier-modal.component';
import { finalize } from 'rxjs/operators';
@Component({
    templateUrl: './supplier-index.component.html',
    animations: [appModuleAnimation()],
    selector: 'supplier-index',
    encapsulation: ViewEncapsulation.None
})
export class SupplierComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('createOrEditSupplierModal') createOrEditSupplierModal: CreateOrEditSupplierModalComponent;
    _type: StockType;
    _edit;
    @Input() set stocktype(val) {
        if (val) {
            this._type = val;
        }
    }
    @Input() set canedit(val) {
        if (val) {
            this._edit = val;
        }
    }

    @Output() modalSelect = new EventEmitter<Stock_SupplierListDto>();
    anyncing = false;
    f_SupplierName;
    f_SupplierShortName;
    selectedValue: Stock_SupplierListDto;
    extensions: Array<ListDtoExtensionsItem>;
    constructor(injector: Injector, private _supplierServiceProxy: StockZT_SupplierServiceProxy) {
        super(injector);
    }

    ngOnInit() {
    }
    rowSelect() {
        this.modalSelect.emit(this.selectedValue);
    }
    delete() {
        if (this.selectedValue.id == null) {
            this.notify.error(this.l('请选择要删除的信息'));
        } else {
            this.message.confirm('确认删除该条数据吗？', isConfirmed => {
                if (isConfirmed) {
                    this._supplierServiceProxy.deleteSupplier(this.selectedValue.id).subscribe(() => {
                        this.notify.success(this.l('SuccessfullyDeleted'));
                        this.getBaseField();
                    });
                }
            });
        }
    }
    onUpload(event) {
        // let list = event as Array<any>;
        let list = event.fileData[0].sheetJson;
        console.log(list)
        let customerImportIntoList = list.map(el => {
            return {
                f_SupplierName: el.供应商名称,
                f_SupplierShortName: el.供应商简称,
                f_SupplierGroup: el.供应商组,
                f_SupplierCurrency: el.币种,
            };
        }) as Array<Stock_SupplierEditDto>;
        this.message.confirm(
            '确认导入这' + customerImportIntoList.length + '条数据吗？',
            this.l('AreYouSure'),
            async (isConfirmed) => {
                if (isConfirmed) {
                    this.primengTableHelper.showLoadingIndicator()
                    this._supplierServiceProxy
                        .importSupplier(customerImportIntoList)
                        .pipe(finalize(() => {
                            this.primengTableHelper.hideLoadingIndicator()
                        }))
                        .subscribe(result => {
                            //this.getModels();
                            this.notify.success('导入供应商成功');
                            this.getBaseField()
                            //this.primengTableHelper.hideLoadingIndicator();
                        });
                }
            }
        );
    }
    getBaseField(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);

            return;
        }
        this.primengTableHelper.showLoadingIndicator();
        this._supplierServiceProxy
            .getSupplierListExtension(
                this.f_SupplierName,
                this.f_SupplierShortName,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.extensions = result.items;
                this.primengTableHelper.totalRecordsCount = result.dtos.totalCount;
                this.primengTableHelper.records = result.dtos.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    start() {
        if (this.selectedValue.id == null) {
            this.notify.error(this.l('请选择要启用的信息'));
        } else {
            this.message.confirm('确认启用该条数据吗？', isConfirmed => {
                if (isConfirmed) {
                    this._supplierServiceProxy.startSupplier(this.selectedValue.id).subscribe(() => {
                        this.notify.success(this.l('SuccessfullyDeleted'));
                        this.getBaseField();
                    });
                }
            });
        }
    }
    stop() {
        if (this.selectedValue.id == null) {
            this.notify.error(this.l('请选择要停用的信息'));
        } else {
            this.message.confirm('确认停用该条数据吗？', isConfirmed => {
                if (isConfirmed) {
                    this._supplierServiceProxy.stopSupplier(this.selectedValue.id).subscribe(() => {
                        this.notify.success(this.l('SuccessfullyDeleted'));
                        this.getBaseField();
                    });
                }
            });
        }
    }
}
